public class 第六题 {
    //public static int[] directionArray = {3, 4, 5};
    public static int Anscount;
    public static int[][] directionArray = {
            {1,0},
            {-1,0},
            {0,1},
            {0,-1},
            {-1,-1},
            {1,1},
            {-1,1},
            {1,-1}
    };

    public static void main(String[] args) {
        getPermutation("0123456789");
        System.out.println();
        System.out.println(Anscount);
    }

    public static void getPermutation(String A) {
        char[] array = A.toCharArray();
        getPermutationCore(array, 0);
    }

    public static void getPermutationCore(char[] array, int k) {
        if (k == array.length) {
            int count=0;
            int[][] intAarray = new int[3][4];
            intAarray[0][0] = 100;
            intAarray[2][3] = 100;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 4; j++) {
                    if((i==2&&j==3)||(i==0&&j==0)){
                        continue;
                    }
                    intAarray[i][j] = Integer.parseInt(array[count++] + "");
                }
            }

            if (check(intAarray)) {
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 4; j++) {
                        System.out.print(intAarray[i][j] + " ");
                    }
                    System.out.println();
                }
                System.out.println("----------------------------");
                Anscount++;
            }

            return;
        }

        for (int i = k; i < array.length; i++) {
            swap(array, i, k);
            getPermutationCore(array, k + 1);
            swap(array, i, k);
        }

    }

    public static void swap(char[] arr, int k, int i) {
        char temp = arr[k];
        arr[k] = arr[i];
        arr[i] = temp;
    }

    public static boolean check(int[][] intAarray) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 8; k++) {
                    if((i+directionArray[k][0]>=0&&i+directionArray[k][0]<=2)&&(j+directionArray[k][1]>=0&&j+directionArray[k][1]<=3)){
                        if(Math.abs(intAarray[i][j]-intAarray[i+directionArray[k][0]][j+directionArray[k][1]])==1){
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}
/*
* 1 3 3 4
* 5 4 7 8
* 9 1 1 1
* */